﻿using System;
using System.Collections.Generic;
using System.Text;
using DownloadMag.Core;

namespace DownloadMag
{

    #region 序列化存储：下载任务列表

    /// <summary>
    /// 下载任务列表
    /// 用于序列化存储任务信息
    /// </summary>
    public class DTaskDataList
    {

        public DTaskDataList()
        {
            m_TaskDataList = new List<DTaskData>();
        }

        [System.Xml.Serialization.XmlArrayItem("TaskList", typeof(DTaskData))]
        private List<DTaskData> m_TaskDataList;

        public List<DTaskData> TaskDataList
        {
            get { return m_TaskDataList; }
            set { m_TaskDataList = value; }
        }

    }

    #endregion

    #region 任务状态

    /// <summary>
    /// 下载任务状态
    /// </summary>
    public enum DTaskState
    {
        /// <summary>
        /// 停止
        /// </summary>
        Stopped = 0,
        /// <summary>
        /// 已启动
        /// </summary>
        Started,
        /// <summary>
        /// 等待
        /// </summary>
        Waiting,
        /// <summary>
        /// 已完成
        /// </summary>
        Completed,
        /// <summary>
        /// 失败
        /// </summary>
        Failed,
        /// <summary>
        /// 取消
        /// </summary>
        Aborted
    }

    /// <summary>
    /// 下载线程状态
    /// </summary>
    public enum WorkThreadState
    {
        /// <summary>
        /// 停止
        /// </summary>
        Stopped = 0,
        /// <summary>
        /// 已启动
        /// </summary>
        Started,
        /// <summary>
        /// 已完成
        /// </summary>
        Completed,
        /// <summary>
        /// 失败
        /// </summary>
        Failed
    }
    #endregion

    #region 事件相关定义
    public class TaskInitializedEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileInfo">下载文件信息</param>
        public TaskInitializedEventArgs(RemoteFileInfo fileInfo)
        {
            m_RemoteFile = fileInfo;
        }

        private RemoteFileInfo m_RemoteFile;
        /// <summary>
        /// 下载数据大小
        /// </summary>
        public RemoteFileInfo RemoteFile
        {
            get { return m_RemoteFile; }
            set { m_RemoteFile = value; }
        }
    }
    public class WriteToStreamEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="length">数据大小</param>
        public WriteToStreamEventArgs(long length)
        {
            m_length = length;
        }

        private long m_length;
        /// <summary>
        /// 数据大小
        /// </summary>
        public long Length
        {
            get { return m_length; }
            set { m_length = value; }
        }
    }
    public class TaskThreadErrorEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="error">捕获的异常</param>
        public TaskThreadErrorEventArgs(Exception error)
        {
            m_Error = error;
        }

        private Exception m_Error;
        /// <summary>
        /// 错误信息
        /// </summary>
        public Exception Error
        {
            get { return m_Error; }
            set { m_Error = value; }
        }
    }
    public class TaskErrorEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtc">发生错误的分段</param>
        /// <param name="error">捕获的异常</param>
        public TaskErrorEventArgs(DTaskConfig dtc, Exception error)
        {
            m_Error = error;
            m_ErrorThread = dtc;
        }

        private Exception m_Error;
        private DTaskConfig m_ErrorThread;
        /// <summary>
        /// 错误信息
        /// </summary>
        public Exception Error
        {
            get { return m_Error; }
            set { m_Error = value; }
        }
        /// <summary>
        /// 发生错误的分段
        /// </summary>
        public DTaskConfig ErrorThread
        {
            get { return m_ErrorThread; }
            set { m_ErrorThread = value; }
        }
    }
    public class TaskStateChangedEventArgs : DTaskEventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="old_state">旧的状态</param>
        /// <param name="new_statue">新的状态</param>
        public TaskStateChangedEventArgs(DTaskState oldState, DTaskState newState)
        {
            m_OldState = oldState;
            m_NewState = newState;
        }

        private DTaskState m_OldState;
        private DTaskState m_NewState;
        /// <summary>
        /// 旧的状态
        /// </summary>
        public DTaskState OldState
        {
            get { return m_OldState; }
            set { m_OldState = value; }
        }
        /// <summary>
        /// 新的状态
        /// </summary>
        public DTaskState NewState
        {
            get { return m_NewState; }
            set { m_NewState = value; }
        }
    }
    public class DTaskEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        public DTaskEventArgs()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancel">是否取消事件</param>
        public DTaskEventArgs(bool cancel)
        {
            m_Cancel = cancel;
        }

        private bool m_Cancel;
        /// <summary>
        /// 是否取消事件
        /// </summary>
        public bool Cancel
        {
            get { return m_Cancel; }
            set { m_Cancel = value; }
        }
    }
    #endregion

}
